package com.fwtai.api.controller;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.entity.ReqPage;
import com.fwtai.service.api.ApiUserService;
import com.fwtai.tool.ToolClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * app端用户中心
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2020-03-24 12:36
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Api(tags = "账号|用户中心,token选填",description = "我的|账号信息")
@RestController
@RequestMapping(ConfigFile.guest_v10 + "user")
public class UserController{

  @Resource
  private ApiUserService apiUserService;

  //放行对外提供注册接口url
  @ApiOperation(value = "用户注册,token不填", notes = "注册接口")
  @PostMapping(value = "/register")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "username", value = "登录账号(手机号)", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "user_password", value = "账号密码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "verify_password", value = "确认密码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "code", value = "手机验证码", dataType = "String", paramType = "query", required = true)
  })
  public void register(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.add(request),response);
  }

  @ApiOperation(value = "编辑个人信息,token必填", notes = "编辑信息")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "real_name", value = "真实姓名", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "sex", value = "性别,性别(1男;2女;3未知),默认是未知", dataType = "int", paramType = "query",defaultValue = "3", required = true),
    @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "province_id", value = "省主键的kid", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "province_name", value = "省名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "city_id", value = "市主键的kid", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "city_name", value = "市名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "county_id", value = "区县主键的kid", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "county_name", value = "区县名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "address", value = "详细地址", dataType = "String", paramType = "query", required = false)
  })
  @PostMapping("/edit")
  public void edit(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.edit(request),response);
  }

  @ApiOperation(value = "当前登录人或他人查看用户资料信息,token选填", notes = "用于查看普通用户资料|编辑资料前获取个人信息,若是查看普通账号资料需要传userId")
  @ApiImplicitParam(name = "userId", value = "被查看的普通用户userId", dataType = "String", paramType = "query", required = false)
  @GetMapping("/queryById")
  public void queryById(final String userId,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.queryUserInfo(userId),response);
  }

  @ApiOperation(value = "律师列表-获取个人信息,有返回是否关注被查看用户,token选填", notes = "被查看的律师的userId,为空时查看自己的信息")
  @ApiImplicitParam(name = "userId", value = "被查看的律师或普通用户的userId", dataType = "String", paramType = "query", required = true)
  @GetMapping("/queryInfoById")
  public void queryInfoById(final String userId,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.queryInfoById(userId),response);
  }

  @ApiOperation(value = "更新编辑个人头像,token必填", notes = "编辑个人头像,当code为198时引导或跳转'编辑资料'")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "portrait", value = "照片头像",dataType ="图片文件",allowMultiple = true, paramType = "query", required = true)
  })
  @PostMapping("/portrait")
  public void portrait(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.editPortrait(request),response);
  }

  @ApiOperation(value = "申请代理，申请合伙人,token必填", notes = "申请合伙人,需要填写申请信息'")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "real_name", value = "真实姓名", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "id_card", value = "身份证号码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "country_id", value = "国家id,取值从数据字典的api的getCountry", dataType = "long", paramType = "query", required = true),
    @ApiImplicitParam(name = "country_name", value = "国家名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "type", value = "代理类型", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "province_id", value = "省主键的kid", dataType = "long", paramType = "query", required = true),
    @ApiImplicitParam(name = "province_name", value = "省名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "city_id", value = "市主键的kid", dataType = "long", paramType = "query", required = true),
    @ApiImplicitParam(name = "city_name", value = "市名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "county_id", value = "区县主键的kid", dataType = "long", paramType = "query", required = true),
    @ApiImplicitParam(name = "county_name", value = "区县名称", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "address", value = "详细地址", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "phone", value = "联系电话", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "code", value = "扫码获得的推广码|邀请码", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "recommend_code", value = "推荐人的推广码|邀请码", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "details", value = "申请内容信息", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/addPartner")
  public void addPartner(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.addPartner(request),response);
  }

  /**获取绑定微信或是否已设置的登录密码*/
  @ApiOperation(value = "获取绑定微信或是否已设置的登录密码,token必填", notes = "获取是否已绑定微信或设置登录密码,当openid为0时说明未绑定微信号;pwd为0时说明未设置登录密码")
  @GetMapping("/getUserOptions")
  public void getUserOptions(final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getUserOptions(),response);
  }

  /**用户修改密码*/
  @ApiOperation(value = "用户修改密码,token必填", notes = "修改之前的密码")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "original_password", value = "原密码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "new_password", value = "新密码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "verify_password", value = "确认密码", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping(value = "/modify")
  public void modify(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.updatePassword(new PageFormData(request)),response);
  }

  /**忘记密码*/
  @ApiOperation(value = "重置密码,token不填", notes = "忘记密码,重新设置一个新的登录密码")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "password", value = "新密码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "verify", value = "确认密码", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping(value = "/reset")
  public void reset(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.updateForget(new PageFormData(request)),response);
  }

  @ApiOperation("更换手机账号,token必填")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "phone", value = "新的登录账号(手机号)", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "code", value = "短信验证码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "verify", value = "当前返回的code为198时若是继续绑定则传此值1", dataType = "int", paramType = "query", required = false)
  })
  @PostMapping("/updatePhone")
  public void updatePhone(final String phone,final String code,final String verify,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.updatePhone(phone,code,verify),response);
  }

  @ApiOperation("更换微信号,token必填")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "code", value = "微信的code", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/updateOpenid")
  public void updateOpenid(final String code,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.updateOpenid(code),response);
  }

  @ApiOperation(value = "查询是否有未支付的订单,token必填", notes = "查询是否有未支付的订单,提示用户取消订单或支付订单")
  @GetMapping("/getNonPayment")
  public void getNonPayment(final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getNonPayment(),response);
  }

  @ApiOperation(value = "通过邀请码查看邀请人信息,token必填", notes = "查看邀请人信息")
  @ApiImplicitParam(name = "code", value = "邀请码", dataType = "String", paramType = "query", required = true)
  @GetMapping("/getInviteInfo")
  public void getInviteInfo(final String code,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getInviteInfo(code),response);
  }

  //验证登录人的代理、门店审核情况
  @ApiOperation(value = "验证登录人的代理、门店审核情况,token必填", notes = "验证登录人的代理、门店审核情况，未登录不做判断")
  @GetMapping("/getUserAuthInfo")
  public void getUserAuthInfo(final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getUserAuthInfo(),response);
  }

  //获取消费者获取自己的优惠券
  @ApiOperation(value = "获取我的优惠券", notes = "获取消费者获取自己的优惠券")
  @ApiImplicitParam(name = "status", value = "兑换状态(1=待兑换;2=已兑换;3已失效过期)", dataType = "int", paramType = "query", required = false)
  @GetMapping("/getMySecurities")
  public void getMySecurities(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getMySecurities(request),response);
  }

  //获取直营门店自己发放的优惠券
  @ApiOperation(value = "获取直营门店自己发放的优惠券", notes = "获取直营门店自己发放的优惠券")
  @ApiImplicitParam(name = "status", value = "兑换状态(1=待兑换;2=已兑换;3已失效过期)", dataType = "int", paramType = "query", required = false)
  @GetMapping("/getStoreSecurities")
  public void getStoreSecurities(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getStoreSecurities(request),response);
  }

  //店主扫码查询优惠券的信息
  @ApiOperation(value = "店主扫码查询优惠券的信息,token必填", notes = "店主扫码查询优惠券的信息")
  @GetMapping("/getSecuritiesById")
  @ApiImplicitParam(name = "kid", value = "优惠券kid", dataType = "long", paramType = "query", required = true)
  public void getSecuritiesById(final String kid,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getSecuritiesById(kid),response);
  }

  /**根据的优惠卷id查询订单详情*/
  @ApiOperation(value = "根据的优惠卷id查询订单详情", notes = "根据的优惠卷id查询订单详情")
  @ApiImplicitParam(name = "id", value = "被查看的kid", dataType = "long", paramType = "query", required = true)
  @GetMapping("/getSecuritiesOrder")
  public void getSecuritiesOrder(final Long id,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getSecuritiesOrder(id),response);
  }

  //店主确认核销优惠卷
  @ApiOperation(value = "店主确认核销优惠券,区分单件商品或订单核销,token必填", notes = "店主确认核销优惠券,区分单件商品或订单核销")
  @PostMapping("/verifySecurities")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "kid", value = "优惠券kid", dataType = "long", paramType = "query", required = true)
  })
  public void verifySecurities(final String kid,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.verifySecurities(kid),response);
  }

  //申请兑换积分(积分提现申请)
  @ApiOperation(value = "申请兑换积分(积分提现申请),token必填", notes = "申请兑换积分(积分提现申请),提交申请金额")
  @PostMapping("/addUserAccountOut")
  @ApiImplicitParam(name = "value", value = "金额", dataType = "String", paramType = "query", required = true)
  public void addUserAccountOut(final String value,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.addUserAccountOut(value),response);
  }

  //申请兑换积分(积分提现申请)列表
  @ApiOperation(value = "申请兑换积分(积分提现申请)列表", notes = "申请兑换积分(积分提现申请)列表")
  @ApiImplicitParam(name = "state", value = "兑换状态（1待兑换；2已兑换；3已拒绝）,默认是全部", dataType = "int", paramType = "query", required = false)
  @GetMapping("/getApplyRecord")
  public void getApplyRecord(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getApplyRecord(request),response);
  }

  //积分收益明细列表
  @ApiOperation(value = "积分收益明细列表", notes = "积分收益明细列表")
  @ApiImplicitParam(name = "enabled", value = "是否已结算（1已结算；0待结算）,不传值默认是全部", dataType = "int", paramType = "query", required = false)
  @GetMapping("/listIntegral")
  public void listIntegral(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.listIntegral(request),response);
  }

  //获取我邀请的人(代理)列表
  @ApiOperation(value = "获取我邀请的人(代理)带来的收益列表,token必填", notes = "获取我邀请的人(代理)列表,获取我推荐的代理列表")
  @GetMapping("/listReferrerUser")
  public void listReferrerUser(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.listReferrerUser(request),response);
  }

  @ApiOperation(value = "获取我的用户积分|待兑换|已兑换|已结算|待结算", notes = "获取我的用户积分（用户账户）,其中total是总额;apply里的state含1待兑换；2已兑换;account的enabled是1已结算；0待结算")
  @GetMapping("/getIntegralAccount")
  public void getIntegralAccount(final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getIntegralAccount(),response);
  }

  @ApiOperation(value = "获取我的推荐的人的总人数,token必填", notes = "获取我的推荐的人的总人数")
  @GetMapping("/getReferrerAccount")
  public void getReferrerAccount(final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getReferrerAccount(),response);
  }

  @ApiOperation(value = "用户查询自己的推荐积分列表(推荐人的订单收益列表),可指定被推荐者", notes = "用户查询自己的推荐积分列表(推荐人的订单收益列表),可指定被推荐者")
  @ApiImplicitParam(name = "userId", value = "被查看的userId", dataType = "String", paramType = "query", required = true)
  @GetMapping("/getReferrerScoreList")
  public void getReferrerScoreList(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getReferrerScoreList(request),response);
  }

  @ApiOperation(value = "区代用户查询门店分组收入", notes = "用户查询分组收入 未完成，暂时废弃")
  @ApiImplicitParam(name = "userId", value = "指定用户ID,为空代表自己", dataType = "String", paramType = "query", required = false)
  @GetMapping("/getMyGroupIncome")
  public void getMyGroupIncome(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getMyGroupIncome(request),response);
  }

  @ApiOperation(value = "用户查询分组收入(按区域划分查看)", notes = "用户查询分组收入(按区域划分查看)")
  @ApiImplicitParams ({
    @ApiImplicitParam(name = "userId", value = "指定用户ID,为空代表自己", dataType = "String", paramType = "query", required = false),
    @ApiImplicitParam(name = "cityId", value = "指定市级区域ID", dataType = "String", paramType = "query", required = false)
  })
  @GetMapping("/getMyGroupArea")
  public void getMyGroupArea(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getMyGroupArea(request),response);
  }

  @ApiOperation(value = "查询区代用户门店列表", notes = "查询区代用户门店列表")
  @ApiImplicitParam(name = "userId", value = "指定区代用户ID,为空代表自己", dataType = "String", paramType = "query", required = false)
  @GetMapping("/getAreaStoreList")
  public void getAreaStoreList(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getAreaStoreList(request),response);
  }

  @ApiOperation(value = "门店审核列表", notes = "门店审核列表,其中user_id是区代的userId,uid是门店的门店申请人用户id")
  @ApiImplicitParams ({
    @ApiImplicitParam(name = "type", value = "请求类型:1待认证;2已通过;3未通过;8全部", dataType = "int", paramType = "query", required = true),
    @ApiImplicitParam(name = "name", value = "门店名称", dataType = "String", paramType = "query", required = false)
  })
  @GetMapping("/getStoreList")
  public void getStoreList(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getStoreList(request),response);
  }

  @ApiOperation(value = "门店审核", notes = "门店审核")
  @ApiImplicitParams ({
    @ApiImplicitParam(name = "type", value = "审核结果:2同意申请(已通过);3拒绝申请(未通过);", dataType = "int", paramType = "query", required = true),
    @ApiImplicitParam(name = "kid", value = "门店的kid", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "userId", value = "区代的userId及门店申请人uid,从接口getStoreList返回的数据", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/updateStoreStatus")
  public void updateStoreStatus(final String type,final String kid,final String uid,final String userId,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.updateStoreStatus(type,kid,uid,userId),response);
  }

  @ApiOperation(value = "门店详情", notes = "通过门店kid查看门店详情,若返回的code是198时则两个按钮不可用")
  @ApiImplicitParams ({
    @ApiImplicitParam(name = "kid", value = "门店的kid", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "userId", value = "区代的userId,从接口getStoreList返回的数据", dataType = "String", paramType = "query", required = true)
  })
  @GetMapping("/getStoreInfo")
  public void getStoreInfo(final String kid,final String userId,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getStoreInfo(kid,userId),response);
  }

  @ApiOperation(value = "用户查询自己的积分统计|积分收益明细，按年月日统计", notes = "用户查询自己的积分统计|积分收益明细，按年月日统计")
  @ApiImplicitParam(name = "type", value = "type=1为按年统计，type=2为按月统计，type=3为按日统计", dataType = "int", paramType = "query", required = true)
  @GetMapping("/getIntegralInfo")
  public void getIntegralInfo(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getIntegralInfo(request),response);
  }

  @ApiOperation(value = "用户查询自己的积分的收入和支出明细列表", notes = "用户查询自己的积分的收入和支出明细列表")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "type", value = "type=1为按年统计，type=2为按月统计，type=3为按日统计", dataType = "int", paramType = "query", required = false),
    @ApiImplicitParam(name = "timestamp", value = "统计时间段", dataType = "long", paramType = "query", required = false)
  })
  @GetMapping("/listIntegralInfo")
  public void listIntegralInfo(final ReqPage reqPage,final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.listIntegralInfo(request),response);
  }

  @ApiOperation(value = "积分支出记录详情", notes = "积分支出记录详情")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "type", value = "支出类型（1兑换|提现；2采购进货;3积分商城购买商品）", dataType = "int", paramType = "query", required = true),
    @ApiImplicitParam(name = "value", value = "支出记录id（当category=1时值是user_out_record的kid；为2时是user_integral_buy的out_trade_no；为3时积分商城的订单编号）", dataType = "String", paramType = "query", required = true)
  })
  @GetMapping("/getIntegralDetails")
  public void getIntegralDetails(final String type,final String value,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getIntegralDetails(type,value),response);
  }

  @ApiOperation(value = "积分收入记录详情", notes = "积分收入记录详情",hidden = true)
  @ApiImplicitParams({
    @ApiImplicitParam(name = "type", value = "来源分类（1本金；2分成；3优惠券；4推荐）", dataType = "int", paramType = "query", required = true),
    @ApiImplicitParam(name = "out_trade_no", value = "订单号（goods_order.out_trade_no）", dataType = "String", paramType = "query", required = true)
  })
  @GetMapping("/getIntegralOutTradeNo")
  public void getIntegralOutTradeNo(final String type,final String out_trade_no,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getIntegralDetails(type,out_trade_no),response);
  }
}